package com.powernode.controller;


import com.powernode.common.Result;
import com.powernode.domain.SysDictData;
import com.powernode.service.SysDictDataService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/sysDictData")
public class SysDictDataController extends BaseController{
    @Resource
    private SysDictDataService sysDictDataService;


    /**
     * 添加字典数据
     * POST http://127.0.0.1:8080/sysDictData/save
     * 接口ID：60111928
     * 接口地址：https://www.apifox.cn/web/project/{{projectId}}/apis/api-{{id}}
     */
    @PostMapping("/save")
    public Result<?> save(@RequestBody SysDictData sysDictData){
        sysDictData.setCreateBy(getUsername());
        sysDictData.setCreateTime(new Date());
        sysDictDataService.insert(sysDictData);
        return new Result("添加成功");
    }

    /**
     * 根据ID查询字典数据
     * GET http://127.0.0.1:8080/sysDictData/get/{dictCode}
     * 接口ID：60111930
     * 接口地址：https://www.apifox.cn/web/project/{{projectId}}/apis/api-{{id}}
     */
    @GetMapping("/get/{dictId}")
    public Result<?> get(@PathVariable Long dictId){
        SysDictData SysDictData = sysDictDataService.selectByPrimaryKey(dictId);
        return new Result<>(SysDictData);
    }

    /**
     * 修改字典数据
     * PUT http://127.0.0.1:8080/sysDictData/update
     * 接口ID：60111929
     * 接口地址：https://www.apifox.cn/web/project/{{projectId}}/apis/api-{{id}}
     */
    @PutMapping("/update")
    public Result<?> update(@RequestBody SysDictData sysDictData){
        sysDictData.setUpdateBy(getUsername());
        sysDictData.setUpdateTime(new Date());
        sysDictDataService.updateByPrimaryKey(sysDictData);
        return new Result<>("修改成功");
    }

    /**
     * 删除字典数据
     * DELETE http://127.0.0.1:8080/sysDictData/delete/{dictCode}
     * 接口ID：60111931
     * 接口地址：https://www.apifox.cn/web/project/{{projectId}}/apis/api-{{id}}
     */
    @DeleteMapping("/delete/{dictId}")
    public Result delete(@PathVariable Long dictId){
        sysDictDataService.deleteByPrimaryKey(dictId);
        return new Result("删除成功");
    }

    /**
     * 根据字典类型获取字典数据
     * GET http://127.0.0.1:8080/sysDictData/getDictDataByDictType/{dictType}
     * 接口ID：60111932
     * 接口地址：https://www.apifox.cn/web/project/{{projectId}}/apis/api-{{id}}
     */
    @GetMapping("/getDictDataByDictType/{dictType}")
    public Result getDictDataByDictType(@PathVariable String dictType){
        List<SysDictData> sysDictData =sysDictDataService.queryAllDataByType(dictType);
        return new Result(sysDictData);
    }
}
